<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OfficeSuppliesController extends Controller
{

	//办公列表显示
	public function getIndex(Request $request)
	{
		if($request['name']){
			$office = DB::table('officesupplies')
			->where('OfficeSupplies','like','%'.$request['name'].'%')
			->paginate(10);
		}else{
			$office = DB::table('officesupplies')
			->paginate(3);
		}
		// dd($office);
		$info = $request->all();
		return view('Lzk.OfficeSupplies.OfficeSupplies',['office'=>$office,'info'=>$info]);	
	}

	//领用办公用品添加
	public function getAdd()
	{
		$add = DB::table('goods')->get();
		// dd($add);
		return view('Lzk.OfficeSupplies.AddOfficeSupplies',['add'=>$add]);
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$data = $request->only('OfficeSupplies','PersonalName','Number','Operator','Remarks');	
		$pname = $data['PersonalName'];
		$name = DB::table('person')->where('PersonName',$pname)->first();
		if(!$name){
			echo '<script>alert("请填写正确的员工名");window.location.href="/OfficeSupplies/add";</script>';
			die;
		}

		$name = $data['OfficeSupplies'];

		$goods = DB::table('goods')->where('GoodsName',$data['OfficeSupplies'])->get();
		//查看库存数量
		$num = $goods[0]['GoodsNumber'];
		if($data['Number'] > $num){
			echo '<script>alert("没那么多,少领点");window.location.href="/OfficeSupplies/add";</script>';
			die;
		}

		$good = $goods[0]['GoodsNumber'] - $data['Number'];
		
		$del = DB::select("update goods set GoodsNumber={$good} where GoodsName = '{$name}'");


		$time = time();
		$data['Date'] = $time;
		$data['Sfgh'] = 1;

		$res = DB::table('officesupplies')->insert($data);
		if($res){
			return redirect('/OfficeSupplies');
		}else{
			return back();
		}
	}

	//解析修改页面
	public function getEdit($id)
	{
		// echo $id;
		$edit = DB::table('goods')->get();
		$office = DB::table('officesupplies')->where('OfficeSuppliesID',$id)->first();
		return view('Lzk.OfficeSupplies.EditOfficeSupplies',['office'=>$office,'edit'=>$edit]);
	}

	//执行修改
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		$id = $request->only('OfficeSuppliesID');
		$res = $request->only('OfficeSupplies','PersonalName','Number','Sfgh','Operator','Remarks');
		$time = time();
		$res['Date'] = $time;
		$state = $res['Sfgh'];
		$num = $res['Number'];
		$name = $res['OfficeSupplies'];
		// dd($num);
		if($state == 2){
			$goods = DB::table('goods')->where('GoodsName',$res['OfficeSupplies'])->get();
			$gnum = $num + $goods[0]['GoodsNumber'];
			$del = DB::select("update goods set GoodsNumber={$gnum} where GoodsName = '{$name}'");
		}

		


		$ress = DB::table('officesupplies')->where('OfficeSuppliesID',$id)->update($res);
		if($ress){
			return redirect('/OfficeSupplies');
		}else{
			return back();
		}
	}

	//查看
	public function getLook($id)
	{
		$look = DB::table('officesupplies')->where('OfficeSuppliesID',$id)->first();
		return view('Lzk.OfficeSupplies.OfficeSuppliesDetail',['look'=>$look]);
	}

	public function getDel($id)
	{
		$del = DB::select("delete from officesupplies where OfficeSuppliesID = '{$id}'");
		if($del){
			return redirect('/OfficeSupplies');
		}else{
			return back();
		}
	}
}
